package com.zhugang.week03;

import java.util.Stack;

/**
 * @program algorithms
 * @description: CQueue
 * @author: chanzhugang
 * @create: 2022/06/04 14:39
 */
public class CQueue {


    /**
     * 剑指offer 09.用两个栈实现队列
     * <p>
     */

    Stack<Integer> stack = new Stack<>();
    Stack<Integer> tempStack = new Stack<>();

    public CQueue() {

    }

    /**
     * 入队
     *
     * @param value
     */
    public void appendTail1(int value) {
        stack.push(value);
    }

    /**
     * 出队
     * 解法1：入队直接入；出队用两个栈倒腾：栈A元素全部压入栈B,弹出栈B栈顶元素，栈B全部元素压回栈A
     *
     * @return
     */
    public int deleteHead1() {
        if (stack.empty()) {
            return -1;
        }
        while (!stack.empty()) {
            tempStack.push(stack.pop());
        }
        Integer value = tempStack.pop();
        while (!tempStack.empty()) {
            stack.push(tempStack.pop());
        }
        return value;
    }

    /**
     * 解法2：出队直接出；入队用两个栈倒腾：
     *
     * @param value
     */
    public void appendTail12(int value) {
        while (!stack.empty()) {
            tempStack.push(stack.pop());
        }
        stack.push(value);
        while (!tempStack.empty()) {
            stack.push(tempStack.pop());
        }
    }

    public int deleteHead2() {
        if (stack.empty()) {
            return -1;
        }
        return stack.pop();
    }
}